package myzzyl.controller;



import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import myzzyl.domain.dto.SysRoleDto;
import myzzyl.domain.pojo.SysRole;
import myzzyl.domain.vo.SysRoleVo;
import myzzyl.response.BaseResponse;
import myzzyl.response.PageResponse;
import myzzyl.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Tag(name = "角色相关")
@Slf4j
public class RoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 分页获取所有角色
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    public BaseResponse<PageResponse<SysRole>> list(@PathVariable("pageNum") Integer pageNum,
                                                      @PathVariable("pageSize") Integer pageSize,
                                                      @RequestBody SysRoleDto sysRoleDto) {
        log.info("分页获取所有角色:{}, {}, {}", pageNum, pageSize, sysRoleDto);
        PageResponse<SysRole> response = sysRoleService.pageQuery(pageNum, pageSize, sysRoleDto);
        return BaseResponse.success(response);
    }


    /**
     * 获取所有角色
     */
    @PostMapping("/init-roles")
    public BaseResponse<List<SysRole>> list() {
        log.info("获取所有角色");
        List<SysRole> roleList = sysRoleService.getAll();
        return BaseResponse.success(roleList);
    }


    /**
     * 获取角色绑定的资源
     */
    @GetMapping("/find-checked-resources/{roleId}")
    public BaseResponse<List<String>> getCheckedResources(@PathVariable Long roleId) {
        log.info("获取角色绑定的资源:{}", roleId);
        return BaseResponse.success(sysRoleService.getCheckedResources(roleId));
    }

    /**
     * 获取角色详情
     */
    @GetMapping("/{id}")
    public BaseResponse<SysRole> getInfo(@PathVariable Long id) {
        log.info("获取角色详情:{}", id);
        return BaseResponse.success(sysRoleService.getInfo(id));
    }


    /**
     * 新增角色
     */
    @PostMapping
    public BaseResponse<Boolean> add(@RequestBody SysRoleDto sysRoleDto) {
        log.info("新增角色:{}", sysRoleDto);
        return sysRoleService.add(sysRoleDto) ? BaseResponse.success(true) :
                BaseResponse.error(false);
    }


    /**
     * 编辑角色
     */
    @PutMapping
    public BaseResponse<Boolean> edit(@RequestBody SysRoleDto sysRoleDto) {
        log.info("编辑角色:{}", sysRoleDto);
        return sysRoleService.edit(sysRoleDto) ? BaseResponse.success(true) :
                BaseResponse.error(false);
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/{roleId}")
    public BaseResponse<Boolean> remove(@PathVariable Long roleId) {
        log.info("删除角色:{}", roleId);
        return sysRoleService.remove(roleId) ? BaseResponse.success(true) :
                BaseResponse.error(false);
    }


}
